<template>
	<view v-if="orderList.length==0" class="noData">
		<up-button type="warning" @click="goHome">您没有相关订单,去商城逛逛</up-button>
	</view>
	<view v-else class="order-list">
		<view class="item" v-for="(item,index) in orderList" :key="index">
			<view class="order">
				<view class="order-id">订单编号:{{item.order_id}}</view>
				<view class="order-time">订单时间:{{item.create_time}}</view>
			</view>
			<view class="goods" v-for="(g,i) in item.goods" :key="i" @click="goGoodsDetail(g.goodsId)">
				<image :src="baseUrl+'/'+g.goodsImage"></image>
				<view class="info">
					<view class="name">{{g.goodsName}}</view>
					<view class="num">数量:{{g.quantity}}件</view>
					<view class="price">
						<view class="unit-price">单价:￥{{g.unitPrice}}元</view>
						<view class="sub-total">小计:￥{{g.quantity*g.unitPrice}}</view>
					</view>
				</view>
			</view>
			<view class="address-message">
				<uni-icons size="14" type="location" />
				<view>
					<view>{{item.name}}&emsp;{{item.phone}}</view>
					<view>{{item.city}}{{item.address}}</view>
				</view>
			</view>
			<view class="total">总价:￥{{item.goods[0].amount}}</view>
			<view class="tool">
				<view class="delete-order btn" @click="deleteOrder(item.order_id)">删除订单</view>
				<view v-if="item.is_comment==0" class="comment btn" @click="commentOrder(index)">评价</view>
				<view v-else class="alComment">√已评价</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow
	}
	from "@dcloudio/uni-app"
	import {
		useUserStore
	} from '@/store/index'
	import {
		ref
	} from "vue";
	import {
		get,
		baseUrl,
		post
	} from '@/hooks/req'
	const useUserInfo = useUserStore()
	let orderList = ref([])
	const goHome = () => {
		uni.switchTab({
			url: '/pages/home/home'
		})
	}
	//跳转商品详情
	const goGoodsDetail = (goodsId) => {
		uni.navigateTo({
			url: '/pages/goodsDetail/goodsDetail?goodsId=' + goodsId
		})
	}
	const deleteOrder = (orders_id) => {
		uni.showModal({
			content: "确定删除订单吗",
			success(res) {
				if (res.confirm) {
					post("/orders/delete", orders_id, uni.getStorageSync("token")).then(res => {
						uni.showToast({
							title: "删除成功",
							icon: "none",
						})
						loadOrders()
					})
				}
			}
		})
	}
	const commentOrder = (index) => {
		const goodsInfo = []
		orderList.value[index].goods.forEach((item) => {
			goodsInfo.push({
				order_id: orderList.value[index].order_id,
				goodsId: item.goodsId,
				goodsImage: item.goodsImage,
				goodsName: item.goodsName,
				goodsScore: 0,
				goodsCommment: ''
			})
		})
		uni.navigateTo({
			url: '/pages/comment/comment?goodsInfo=' + encodeURIComponent(JSON.stringify(goodsInfo))
		})
	}
	const loadOrders = () => {
		get("/userOrders/" + useUserInfo.userInfo.id, uni.getStorageSync("token")).then(res => {
			orderList.value = []
			if (res.data.data != null) {
				const data = res.data.data
				data.forEach(d => {
					let order = orderList.value.find(o => o.order_id === d.order_id)
					if (!order) {
						order = {
							order_id: d.order_id,
							create_time: d.create_time,
							name:d.name,
							phone:d.phone,
							city:d.city,
							address: d.address,
							is_comment: d.is_comment,
							goods: []
						}
						orderList.value.push(order)
						orderList.value.reverse()
					}
					order.goods.push(d)
				})
			}
		})
	}
	onShow(() => {
		loadOrders()
	})
	onLoad(() => {
		loadOrders()
	})
</script>

<style lang="scss" scoped>
	.order-list {
		padding: 5px;
		font-size: 12px;
		color: #656565;

		.item {
			margin: 5px;
			border-bottom: 1px solid #eee;

			.order {
				display: flex;
				justify-content: space-between;
			}

			.goods {
				display: flex;
				align-items: center;
				margin-bottom: 5px;

				image {
					width: 80px;
					height: 80px;
				}

				.info {
					margin-left: 5px;

					.name {
						color: black;
						font-weight: bold;
					}

					.num {
						margin: 10px 0;
					}

					.price {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.sub-total {
							margin-left: 20px;
						}
					}
				}
			}
			.address-message{
				display: flex;
				align-items: center;
				}
			.total {
				text-align: right;
				color: #ff6700;
				font-weight: bold;
				padding: 5px 0;
				border-top: 1px solid #eee;
			}

			.tool {
				display: flex;
				justify-content: flex-end;
				margin-bottom: 10px;

				.btn {
					text-align: center;
					height: 20px;
					line-height: 20px;
				}

				.delete-order {
					width: 60px;
					border: 1px solid #eee;
				}

				.comment {
					color: #ff6700;
					width: 50px;
					margin-left: 10px;
					border: 1px solid #ff6700;
				}

				.alComment {
					color: #ff6700;
					width: 50px;
					margin-left: 10px;
				}
			}
		}
	}
</style>
